<?php
namespace Modules\Search\Library;

use Core\Forms\Form;
use Phalcon\Di;
use Modules\Search\Models\Search;

class Common
{
    public static function defaultSearch(Form $form)
    {

        $data = $form->getData();
        $searchProject = Di::getDefault()->getShared('config')->cache('searchProject');

        if (!isset($searchProject[$data['type']]) || !isset($searchProject[$data['type']]['defaultSearch']) || $searchProject[$data['type']]['defaultSearch'] != true) {
            return false;
        }

        $search = Search::find([
            'conditions' => [
                'word' => $data['word']
            ]
        ]);
        if ($search) {
            $search->hot += 1;
        } else {
            $search = new Search();
            $search->project = $data['type'];
            $search->word = $data['word'];
        }
        if ($search->save()) {
            return [
                'for' => 'searchResult',
                'id' => $search->getId(),
                'page' => 1,
            ];
        } else {
            return false;
        }
    }
}